import http from 'axios'
import qs from 'qs'

const debug = process.env.NODE_ENV !== 'production'

http.defaults.baseURL = 'http://zyb.iycgg.com'
http.defaults.timeout = 30 * 1000
// http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
http.defaults.headers.post['Content-Type'] = 'application/json'
// http.defaults.headers.Bearertoken = 'eyJhbGciOiJub25lIn0.eyJzdWIiOiJ6eWJfMHNwZGU3Z3U4ejRuZDk1Yjd3dWpqbmxoIiwicm9sZSI6Ik1FTUJFUiIsImlhdCI6MTUyMzE3NDMwNCwiZXhwIjoxNTI1NzY2MzA0fQ.'

http.interceptors.request.use((config) => {
    if (config.method === 'post') {
        // config.data = qs.stringify(config.data)
        config.headers = { ...config.headers, ...{ 'Authorization': `Bearer ${getUrlParams().token}` } }
    }
    return config
}, (error) => {
    console.log(error)
    return Promise.reject(error)
})

http.interceptors.response.use((res) => {
    return res
}, (error) => {
    console.log(error)
    return Promise.reject(error)
})

function getUrlParams() {
    var url = location.search;
    var theRequest = new Object();
    console.log(url)
    // debugger
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        // str = str.substr(0, str.indexOf('/'))
        var strs = str.split("&");
        for (var i = 0; i < strs.length; i++) {
            theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
        }
    }
    return theRequest;
}

export default http
